#!/usr/bin/env python
#coding=utf-8

import os
import sys
import time
import click
import logging
from datetime import datetime as dt

from lib.xsl import XLSWriter
from lib.common import excel_folder_path, log_info
from lib.qyapi_weixin  import QyWeixin

from accounts import huo_accounts, bian_accounts, okex_accounts, collect_accounts, collect_bat, export_net_value
from onekey import accounts_reduce
from order_oneclick import bina_order, bina_cancel_order, huobi_order, huobi_cancel_order, okex_order,\
        okexft_order, okex_cancel_order, okex_future_cancel


__author__ = "Cap"

qy = QyWeixin()
abspath = os.path.abspath(__file__)

@click.group()
def cli():
    pass

@click.command()
@click.option('--accounts_type',
              '-m',
              default='collect',
              type=click.Choice(['bian', 'huobi', 'okex', 'net_value', 'collect']),
              help='input one of the param which is bina, huobi, okex, collect to check the accounts')    # 限定值
def accounts_collect(accounts_type):
    """账户汇总"""

    bian_list ,huobi_list, okex_list = [], [], []
    # 币安账户
    if accounts_type == 'bian':
        bian_list = bian_accounts()

    # 火币账户
    if accounts_type == 'huobi':
        huobi_list = huo_accounts()

    # Okex账户
    if accounts_type == 'okex':
        okex_list = okex_accounts()


    if accounts_type == 'net_value':
        net_value_export()


    if accounts_type == 'collect':
        if len(bian_list) <= 0:
            bian_list = bian_accounts()

        if len(okex_list) <= 0:
            okex_list = okex_accounts()

        if len(huobi_list) <= 0:
            huobi_list = huo_accounts()

        # 虚拟货币列表
        coin_list = [okex[0] for okex in okex_list]
        coin_list = list(set(coin_list)) # 币种列表去重

        # 账户汇总
        print ('### coin_list:%s'% coin_list)
        collect_accounts(coin_list, bian_list, okex_list, huobi_list)

        export_net_value()

@click.command()
@click.option('--sendmsg', default='Dreamy|Simons|winsen|rickon|jason|Kevin|cap', help='userid for sending message')
def sendmsg_2vic(sendmsg):
    """发送消息账户汇总到企业微信"""
    secret = 'xzNVWx-_Ub_HwDsWZn3ISOjtbRem56Izkg1ZMUW4z1g'
    agentid = 1000003
    netvalue = collect_bat('./ValuationInput.txt')
    resp = qy.sendtxt(secret, agentid, netvalue, sendmsg)
    print('netvalue:%r  resp:%r' % (netvalue, resp))


@click.command()
@click.option('--currency', '-c', default='', help=u'减仓币种')
@click.option('--multiple', '-m',
              type=click.Choice(['1', '1/2', '1/3']),
              help=u'减仓的倍数.')
@click.option('--trading', '-t', default='collect',
              type=click.Choice(['bina', 'huobi', 'okex', 'collect']),
              help=u'减仓的交易所')
def onekey_reduce(currency, multiple, trading):
    """账户一键减仓"""

    if not multiple:
        click.echo(u'### 请选择减仓的倍数')
        return False

    dict = {'1':1, '1/2':0.5, '1/3':1.0/3}
    accounts_reduce(currency, dict.get(multiple), trading)


cli.add_command(accounts_collect)
cli.add_command(sendmsg_2vic)
cli.add_command(onekey_reduce)

if __name__ == "__main__":
    try:
        logging.info('wait...')
        cli()
    except Exception as e:
        #os.system('python %s'% abspath)
        logging.exception('error_msg exit %r', e)




